package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "声音管理")
@RestController
@RequestMapping("/api/album/trackInfo")
public class TrackInfoApiController {

    private final TrackInfoService trackInfoService;

    @Autowired
    public TrackInfoApiController(TrackInfoService trackInfoService) {
        this.trackInfoService = trackInfoService;
    }

    /**
     * 声音上传
     *
     * @param file 上传的文件
     * @return Result
     */
    @GuiguLogin
    @PostMapping("/uploadTrack")
    public Result<JSONObject> uploadTrack(@RequestParam("file") MultipartFile file) {
        return Result.ok(trackInfoService.uploadTrack(file));
    }

    /**
     * 保存声音
     *
     * @param trackInfoVo 声音信息
     * @return Result
     */
    @GuiguLogin
    @PostMapping("/saveTrackInfo")
    public Result<Void> saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    /**
     * 分页查询声音列表
     *
     * @param pageNum        页码
     * @param pageSize       页大小
     * @param trackInfoQuery 查询条件
     * @return 声音列表
     */
    @GuiguLogin
    @PostMapping("/findUserTrackPage/{pageNum}/{pageSize}")
    public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @RequestBody TrackInfoQuery trackInfoQuery) {
        return Result.ok(trackInfoService.findUserTrackPage(pageNum, pageSize, trackInfoQuery));
    }

    /**
     * 删除声音
     *
     * @param trackId 声音id
     * @return Result
     */
    @GuiguLogin
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result<Void> removeTrackInfo(@PathVariable Long trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    /**
     * 修改声音表单回显
     *
     * @param trackId 声音id
     * @return 声音信息
     */
    @GuiguLogin
    @GetMapping("/getTrackInfo/{trackId}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long trackId) {
        return Result.ok(trackInfoService.getById(trackId));
    }

    /**
     * 更新声音信息
     *
     * @param trackInfoVo 前端提交的声音
     * @param trackId     声音id
     * @return Result
     */
    @GuiguLogin
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result<Void> updateTrackInfo(@RequestBody TrackInfoVo trackInfoVo, @PathVariable Long trackId) {
        trackInfoService.updateTrackInfo(trackInfoVo, trackId);
        return Result.ok();
    }

    /**
     * 分页查询专辑的声音列表
     *
     * @param albumId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findAlbumTrackPage/{albumId}/{pageNum}/{pageSize}")
    @GuiguLogin
    public Result findAlbumTrackPage(@PathVariable("albumId") Long albumId, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        return Result.ok(trackInfoService.findAlbumTrackPage(albumId, pageNum, pageSize));
    }
}

